package com.hxd.otc.module.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.my.adapter.SelectBankTypeAdapter;
import com.hxd.otc.module.my.contract.SelectBankTypeContract;
import com.hxd.otc.module.my.presenter.SelectBankTypePresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.widget.RecyclerListDivider;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${选择支付类型界面}
 */
public class SelectBankTypeActivity extends BaseRecyclerActivity<SelectBankTypePresenter, SelectBankTypeAdapter, BankType> implements SelectBankTypeContract.View {

    public static final int REQUEST_CODE_ADD_CARD = 10001;
    public static final String EXTRA_HAS_WEIXIN = "hasWeiXin";
    public static final String EXTRA_HAS_ALIPAY = "hasAlipay";
    private boolean mHasWeiXin;
    private boolean mHasAlipay;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_bank_type;
    }

    @Override
    public SelectBankTypeAdapter createAdapter() {
        return new SelectBankTypeAdapter(this);
    }

    @Override
    protected SelectBankTypePresenter createPresenter() {
        return new SelectBankTypePresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (bundle != null) {
            mHasWeiXin = bundle.getBoolean(EXTRA_HAS_WEIXIN, false);
            mHasAlipay = bundle.getBoolean(EXTRA_HAS_ALIPAY, false);
        }
    }

    @Override
    public RecyclerView.ItemDecoration getDividerItemDecoration() {
        return new RecyclerListDivider(DataUtils.dp2px(getApplicationContext(), 0.5f), ContextCompat.getColor(getApplicationContext(), R.color.color_Line));
    }

    @Override
    public void onItemClick(int position) {
        super.onItemClick(position);
        Bundle bundle = new Bundle();
        BankType bankType = mAdapter.getItem(position);

        if (bankType == BankType.WEXIN && mHasWeiXin) {
            showToast(getString(R.string.you_already_add_weixin));
            return;
        }

        if (bankType == BankType.ALIPAY && mHasAlipay) {
            showToast(getString(R.string.you_already_add_alipay));
            return;
        }

        bundle.putSerializable(AddPayActivity.EXTRA_BANK_TYPE, bankType);
        $startActivityForResult(AddPayActivity.class, bundle, REQUEST_CODE_ADD_CARD);
    }

    @Override
    public boolean isEnableLoadMore() {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_CARD && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }
}
